#
# Filename: VCSLabels.py
# $Id: VCSLabels.py,v 1.2 1999/08/13 06:42:02 toni Exp $
#
# define the functionality to use the dialogs for setting
# and moving a label as defined in SetLabelDialogs.py
#
# Author: Uwe Stieber
# Copyright (c) 1998, 1999 TakeFive Software, Salzburg
#

# import the necessary modules
import string
import os
import vcs
from etgui import *

# Global helper functions
# If these functions are implemented by the current
# used CMVC adaptor itself, then use the CMVC functions
# itself

##################################################################
# transLabel()
##################################################################

if vcs.tool != None and vcs.tool.__dict__.has_key('transLabel'):
	transLabel = vcs.tool.transLabel
else:
	def transLabel(aLabel, transCharacters, replaceCharacters):	
		# translate german 'umlaute'
		tempLabel = aLabel
		tempLabel = string.replace(tempLabel, "ä", "ae")
		tempLabel = string.replace(tempLabel, "ö", "oe")
		tempLabel = string.replace(tempLabel, "ü", "ue")
		tempLabel = string.replace(tempLabel, "ß", "ss")
		# translate Label
		TRANSTABLE = string.maketrans(transCharacters, replaceCharacters)
		return string.translate(aLabel, TRANSTABLE)

##################################################################
# GetHead()
#
# returns the HEAD revision as string
##################################################################
if vcs.tool != None and vcs.tool.__dict__.has_key('GetHead'):
	GetHead = vcs.tool.GetHead
else:
	def GetHead():
		if vcs.tool != None and vcs.tool.__dict__.has_key("log_without_history"):
			log = vcs.tool.log_without_history(vcs.TRUE)
			if not log:
				return ""
			else:
				return log.head_rev()
		else:
			return ""

##################################################################
# setLabelOnVersion()
##################################################################
def setLabelOnVersion(aRepFileList, aFileNameList, aFileRevision):
	if vcs.TEST:
		print "(@B@I%s@I@B)@B@g>>>@x setLabelOnVersion@B" % __name__
		print "(@B@I%s@I@B)\taRepFileList = '@B%s@B'" % (__name__, aRepFileList)
		print "(@B@I%s@I@B)\taFileNameList = '@B%s@B'" % (__name__, aFileNameList)
		print "(@B@I%s@I@B)\taFileRevision = '@B%s@B'" % (__name__, aFileRevision)
		print "(@B@I%s@I@B)\tvcs.tool.__name__ = '@B%s@B'" % (__name__, vcs.tool.__name__)
	# split the lists
	repFileList = string.split(aRepFileList)
	fileList = string.split(aFileNameList)
	if len(fileList) == 1:
		if aFileRevision == '':
			# ok, only one file and no selected revision,
			# we can present the number of the tip / Head revision
			vcs.repositoryFile = repFileList[0]
			vcs.workingFile = fileList[0]
			headRevisionNumber = GetHead()
		else:
			# use the selected revision
			headRevisionNumber = aFileRevision
	else:
		# more than one file, present 'TIP' (Pvcs) or 'HEAD' (RCS and the rest)
		if string.upper(vcs.tool.__name__) == "PVCS":
			headRevisionNumber = "TIP"
		else:
			headRevisionNumber = "HEAD"
	try:
		import SetLabelDialogs
		aLabel, aVersion, floatingFlag, changesetFlag = SetLabelDialogs.Show(headRevisionNumber, vcs.tool.__name__)
		if aLabel != None and aVersion != None:
			rc = doSetLabel(repFileList, transLabel(aLabel, ' :*"', '____'), aVersion, floatingFlag, changesetFlag)
			if rc != 0:
				ShowAlert(eAlertNote, "To view result, click file @B%s@B once again !" % os.path.basename(vcs.workingFile))
	except:
		if vcs.TEST: print "(@B@I%s@I@B)@B@r<<<@x setLabelOnVersion (Exception)@B" % __name__
		import traceback
		traceback.print_exc()
		return
	if vcs.TEST: print "(@B@I%s@I@B)@B@r<<<@x setLabelOnVersion@B" % __name__
	return

##################################################################
# doSetLabel()
##################################################################
def doSetLabel(repFileList, aLabel, aVersion, floatingFlag, changesetFlag):
	if vcs.TEST:
		print "(@B@I%s@I@B)@B@g>>>@x doSetLabel@B" % __name__
		print "(@B@I%s@I@B)\taLabel = '@B@b%s@x@B'" % (__name__, aLabel)
		print "(@B@I%s@I@B)\taVersion = '@B@b%s@x@B'" % (__name__, aVersion)
		print "(@B@I%s@I@B)\tFloating Label = @B@b%d@x@B" % (__name__, floatingFlag)
		print "(@B@I%s@I@B)\tMake Changeset = @B@b%d@x@B" % (__name__, changesetFlag)
	# set the Labels
	for aRepFile in repFileList:
		# set forceFlag
		vcs.forceFlag = 1
		# set symbolName
		if changesetFlag:
			vcs.symbolName = "SNIFF_CHANGESET_" + aLabel
		else:
			vcs.symbolName = aLabel
		# set repositoryFile
		vcs.repositoryFile = aRepFile
		# is it a floating Label
		if floatingFlag:
			mainNumber = string.split(aVersion, '.')
			if len(mainNumber) >= 1:
				revNumber = mainNumber[0]
				for aIndex in range(1,len(mainNumber) - 1):
					revNumber = revNumber + '.' + mainNumber[aIndex]
				revNumber = revNumber + '*'
			else:
				revNumber = "*"
			vcs.revision = revNumber
		else:
			if string.upper(vcs.tool.__name__) == "PVCS":
				if string.upper(aVersion) == "TIP":
					vcs.revision = ""
				else:
					vcs.revision = aVersion
			else:
				if string.upper(aVersion) == "HEAD":
					vcs.revision = ""
				else:
					vcs.revision = aVersion
		#check, if on one revision just ONE Changeset exits
		# !! This is just a limitation of SNiFF+ :-( not of the VCS Tools !!
		forceFail = 0
		rc = 0
		if string.find(vcs.symbolName, "SNIFF_CHANGESET_") != -1:
			if vcs.tool != None and vcs.tool.__dict__.has_key("log_with_history"):
				log = vcs.tool.log_with_history(vcs.TRUE)
				for aSymbolName in log.symbollist():
					if vcs.TEST: print "(@B@I%s@I@B)\t%s" % (__name__, aSymbolName)
					if string.find(aSymbolName, vcs.revision) != -1 and string.find(aSymbolName, "SNIFF_CHANGESET_") != -1:
						forceFail = 1
			else:
				print "(@B@I%s@I@B)@BCan't validate amount of SNiFF+ Changesets for revision %s on file %s @B" % (__name__, vcs.revision, vcs.workingFile)
		if forceFail == 0:
			# call SetSymbol
			rc = 1
			if vcs.tool != None and vcs.tool.__dict__.has_key("SetSymbol"):
				rc = vcs.tool.SetSymbol()
		else:
			ShowAlert(eAlertNote, "There is already @Bone@B SNiFF+ changeset set for revision %s on file %s ! SNiFF+ currently only allows @Bone SNiFF+ changeset per revision@B ! Action aborted !" % (vcs.revision, vcs.workingFile))
	if vcs.TEST: print "(@B@I%s@I@B)@B@r<<<@x doSetLabel@B" % __name__
	return rc

##################################################################
# moveLabelTo()
##################################################################
def moveLabelTo(aRepFile, aFileName, aFileRevision):
	if vcs.TEST:
		print "(@B@I%s@I@B)@B@g>>>@x moveLabelTo@B" % __name__
		print "(@B@I%s@I@B)\taRepFile = '@B%s@B'" % (__name__, aRepFile)
		print "(@B@I%s@I@B)\taFileName = '@B%s@B'" % (__name__, aFileName)
		print "(@B@I%s@I@B)\taFileRevision = '@B%s@B'" % (__name__, aFileRevision)
		print "(@B@I%s@I@B)\taRepositoryFile = '@B%s@B'" % (__name__, vcs.repositoryFile)
		print "(@B@I%s@I@B)\tvcs.tool.__name__ = '@B%s@B'" % (__name__, vcs.tool.__name__)

	# check if a revision is selected (requires history activated) to prevent crash
	if aFileRevision == '':
		ShowAlert(eAlertNote, "No revision selected for file @B'%s'@B" % aFileName)
		return

	if vcs.repositoryFile != aRepFile:
		vcs.repositoryFile = aRepFile

	if vcs.tool != None and vcs.tool.__dict__.has_key("log_with_history"):
		log = vcs.tool.log_with_history(vcs.TRUE)
		if string.upper(vcs.tool.__name__) == "PVCS":
			revisionList = log.revision_numbers()
		else:
			# build revisionList
			revisionList = []
			for aRevision in log.revision_list():
				revisionList.append("%s" % aRevision.rev)
		if vcs.TEST:
			print "(@B@I%s@I@B)\t%s" % (__name__, revisionList)
			print "(@B@I%s@I@B)\tlength of revisionList = '@B%d@B'" % (__name__, len(revisionList))
		symbolList = log.symbollist()
		if vcs.TEST:
			print "(@B@I%s@I@B)\tlength of symbolList = '@B%d@B'" % (__name__, len(symbolList))
		foundLabel = 0
		labelList = []
		for aSymbol in symbolList:
			labelName, labelRev = string.split(aSymbol)
			if labelRev == aFileRevision:
				foundLabel = 1
				labelList.append(labelName[:-1])
		try:
			if foundLabel:
				import SetLabelDialogs
				aRevision, aSelectedLabel = SetLabelDialogs.ShowMoveLabelDialog(aFileName, labelList, revisionList)
				if aRevision != None:
					if vcs.TEST:
						print "(@B@I%s@I@B)\tget Revision from Dialog: '@B%s@B'" % (__name__, aRevision)
						print "(@B@I%s@I@B)\tget Label from Dialog: '@B%s@B'" % (__name__, aSelectedLabel)
					doMoveLabel(aRepFile, aFileName, aSelectedLabel, aRevision)
			else:
				ShowAlert(eAlertNote, "No label is assigned to selected revision @B'%s'@B" % aFileRevision)
		except:
			if vcs.TEST: print "(@B@I%s@I@B)@B@r<<<@x moveLabelTo (Exception)@B" % __name__
			import traceback
			traceback.print_exc()
			return
	else:
		ShowAlert(eAlertNote, "Tool @B%s@B doesn't contains a function called @Blog_with_history@B !" % vcs.tool.__name__)
	if vcs.TEST: print "(@B@I%s@I@B)@B@r<<<@x moveLabelTo@B" % __name__
	return

##################################################################
# doMoveLabel()
##################################################################
def doMoveLabel(aRepFile, aFileName, aLabel, aRevision):
	if vcs.TEST: print "(@B@I%s@I@B)@B@g>>>@x doMoveLabel@B" % __name__
	repFileList = [ aRepFile ]
	rc = doSetLabel(repFileList, aLabel, aRevision, 0, 0)
	if rc != 0:
		ShowAlert(eAlertNote, "To view result, click file @B%s@B once again !" % os.path.basename(aFileName))
	if vcs.TEST: print "(@B@I%s@I@B)@B@r<<<@x doMoveLabel@B" % __name__
